<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	class Admin extends CI_Controller
	{
		private $module = "admin";
		
		public function __construct()
		{
			parent::__construct();
			$this->load->model($this->module.'_model','model');
			$this->template->set_template('admin');
			$this->template->write('title','Nodata CMS');
			$this->template->write_view('menu','BACKEND/menu');
		}
		
		public function index()
		{
			$this->template->write_view('content','BACKEND/index');
			$this->template->render();
		}
		
		public function login()
		{
			if($_POST)
			{
				if($this->model->check_login($this->input->post('username'),$this->input->post('password')))
				{
					$this->session->set_userdata('admin_info',$this->input->post('username'));
					header('Location: '.base_url().'admin');
					exit;
				}
			}
			$this->template->write('content','BACKEND/login');
			$this->template->render();
		}
		
		public function logout()
		{
			$this->session->unset_userdata('admin_info');
			header('Location: '.base_url().'admin/login');
			exit;
		}
		
		public function admin_info()
		{
			if($_POST)
			{
				if(md5($this->input->post('old_password')) == $this->model->admin_info($this->session->userdata('admin_info'))->password){
					if($this->model->update_info($this->session->userdata('admin_info')))
					{
						$this->session->set_flashdata('alert','Thay đổi thông tin thành công!');
						header('Location: '.base_url().'admin/admin_info');
						exit;
					}else{
					
					}
				}
			}else{
				$result = $this->model->admin_info($this->session->userdata('admin_info'));
				$data = array(  
								'result'=>$result,
								'module'=>$this->module
							);
				$this->template->write('content','admin_info',$data);
				$this->template->render();
			}
		}
	}
?>